﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Zyg.SSO.Web.Admin.Account
{
    public partial class AccountRegisterCheck : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!base.Account.HavePermission(Zyg.SSO.Core.Business.Permission.AccountManage))
                {
                    throw new Exception("你无权查看此页面");
                }
                BindData();
            }
        }

        private void BindData()
        {
            IList<Zyg.SSO.Core.Business.Account> accounts = Zyg.SSO.Core.Business.Account.GetAccounts(false, Zyg.SSO.Core.Business.AccountState.Register);

            GridViewAccountList.DataSource = Zyg.SSO.Core.Business.Account.ToDataTable(accounts);
            GridViewAccountList.DataBind();
        }

        protected void BtnPass_Click(object sender, EventArgs e)
        {
            for (int i = GridViewAccountList.Rows.Count - 1; i > -1; i--)
            {
                CheckBox cb = GridViewAccountList.Rows[i].FindControl("CBSelect") as CheckBox;

                 if (cb.Checked)
                 {
                     Guid accountId = new System.Guid(GridViewAccountList.DataKeys[i].Value.ToString());

                     Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(accountId);
                     account.State = Zyg.SSO.Core.Business.AccountState.Active;

                     account.Save();

                     Zyg.SSO.Core.Business.Log log = new Zyg.SSO.Core.Business.Log();
                     log.AccountId = base.Account.Id;
                     log.ModuleId = Zyg.SSO.Core.Business.Module.Account.Id;
                     log.Message = string.Format("管理员 {0} 审核通过了用户 {1} 的注册申请", base.Account.Email, account.Email);

                     log.Save();
                 }
            }

            BindData();
        }

        protected void BtnNo_Click(object sender, EventArgs e)
        {
            for (int i = GridViewAccountList.Rows.Count - 1; i > -1; i--)
            {
                CheckBox cb = GridViewAccountList.Rows[i].FindControl("CBSelect") as CheckBox;

                if (cb.Checked)
                {
                    Guid accountId = new System.Guid(GridViewAccountList.DataKeys[i].Value.ToString());

                    Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(accountId);
                    account.DeleteOnSave();

                    account.Save();

                    Zyg.SSO.Core.Business.Log log = new Zyg.SSO.Core.Business.Log();
                    log.AccountId = base.Account.Id;
                    log.ModuleId = Zyg.SSO.Core.Business.Module.Account.Id;
                    log.Message = string.Format("管理员 {0} 驳回了用户 {1} 的注册申请", base.Account.Email, account.Email);

                    log.Save();
                }
            }

            BindData();
        }
    }
}
